package org.budo.html.editor.handler;

import java.io.InputStream;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.budo.support.lang.util.IoUtil;
import org.budo.support.mvcs.Mvcs;
import org.budo.support.spring.context.aware.BudoApplicationContextAware;
import org.budo.time.Time;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;

/**
 * @author lmw
 */
public abstract class AbstractBudoHtmlEditorHandler implements BudoHtmlEditorHandler {
    /**
     * 静态资源缓存过期时间，单位秒
     */
    private static final Integer RESOURCE_EXPIRES_IN = 5 * 60;

    protected Boolean writeClassPathResource(String resourceName, HttpServletResponse response) {
        InputStream inputStream = this.readClassPathResource(resourceName);

        if (null == inputStream) {
            return false;
        }

        this.writeHeaders(resourceName, response);

        IoUtil.write(inputStream, Mvcs.getOutputStream(response));
        return true;
    }

    protected void writeHeaders(String resourceName, HttpServletResponse response) {
        if (resourceName.endsWith(".css")) {
            response.setContentType("text/css");
        }

        response.setHeader("Cache-Control", "public"); // Cache-Control来控制页面的缓存与否,public:浏览器和缓存服务器都可以缓存页面信息；
        response.setHeader("Pragma", "Pragma"); // Pragma:设置页面是否缓存，为Pragma则缓存，no-cache则不缓存

        Long nowTime = Time.now().toMilliSeconds();
        response.setDateHeader("Last-Modified", nowTime); // Last-Modified:页面的最后生成时间
        response.setDateHeader("Expires", nowTime + (RESOURCE_EXPIRES_IN * 1000)); // Expires:过时期限值
    }

    protected InputStream readClassPathResource(String resourceName) {
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);

        if (null == inputStream) {
            inputStream = this.getClass().getResourceAsStream(resourceName);
        }

        if (null == inputStream) {
            inputStream = this.getClass().getClassLoader().getResourceAsStream(resourceName);
        }
        return inputStream;
    }

    protected MultipartFile getFirstMultipartFile(HttpServletRequest request) {
        MultipartResolver multipartResolver = BudoApplicationContextAware.getBean(MultipartResolver.class);
        MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);

        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        if (fileMap.size() > 1) {
            throw new RuntimeException();
        }

        Entry<String, MultipartFile> next = fileMap.entrySet().iterator().next();
        MultipartFile multipartFile = next.getValue();
        return multipartFile;
    }
}